<template>
	<view class="minh-bg">
		<view class="flex-start px-2 pt-5 pb-3 bg-ff">
			<u-image width="160rpx" height="160rpx" shape="circle" src="/static/images/usertx.svg"></u-image>
			<view class="flex-column pl-1">
				<h2 class="f-w500 pb-m2 font36 col-main">{{ userInfo.NameCn }}</h2>
				<h4 class="font32 col-gray">{{ userInfo.Mobile }}</h4>
				<h4 class="font32 col-gray mt-x1">{{ userInfo.OrgName }}</h4>
			</view>
		</view>
		<u-gap height="30" bg-color="#F6F8FB"></u-gap>
		<view class="px-2 bg-ff fa-user-list">
			<u-cell-group :border="false">
				<u-cell-item title="操作记录" :title-style="titleStyle" hover-class="none" :border-bottom="false" @click="toPage('./record/index')"></u-cell-item>
				<!-- <u-cell-item title="邀请家人一起资产管理资产" :title-style="titleStyle" hover-class="none"></u-cell-item> -->
				<!-- <u-cell-item title="邀请注册手持机账号" :title-style="titleStyle" hover-class="none" :border-bottom="false"></u-cell-item> -->
			</u-cell-group>
		</view>
		<u-gap height="30" bg-color="#F6F8FB"></u-gap>
		<view class="px-2 bg-ff fa-user-list">
			<u-cell-group :border="false">
				<u-cell-item title="修改密码" :title-style="titleStyle" hover-class="none" @click="toPage('../login/resetPassword')"></u-cell-item>
				<u-cell-item title="关于我们" :title-style="titleStyle" hover-class="none" @click="toPage('./about/index')"></u-cell-item>
				<u-cell-item title="检查更新" :title-style="titleStyle" hover-class="none" :border-bottom="false" @click="checkAppUpdata"></u-cell-item>
				<u-cell-item v-if="isAndroid" title="允许显示在应用上" :title-style="titleStyle" hover-class="none" :border-bottom="false" @click="applyOrShowFloatWindow"></u-cell-item>
				<u-cell-item v-if="isAndroid" title="耗电设置" :title-style="titleStyle" hover-class="none" :border-bottom="false" @click="ignoreBattery"></u-cell-item>
				<u-cell-item v-if="isAndroid" title="后台运行" :title-style="titleStyle" hover-class="none" :border-bottom="false" @click="keepLiveSetting"></u-cell-item>
			</u-cell-group>
		</view>
		<u-gap height="232" bg-color="#F6F8FB"></u-gap>
		<view class="py-1 text-center bg-ff flex-center" @click="showLoginOut = !showLoginOut" style="color: #E55D5D;">
			<u-icon name=" iconfont icon-tuichu" size="32"></u-icon>退出登录
		</view>
		<u-modal
			v-model="showLoginOut"
			content="确认退出当前账号？" 
			:show-title="false"
			:title-style="{'font-size': '36rpx', 'color': '#2A2A2A'}"
			:content-style="{'font-size': '36rpx', 'color': '#2A2A2A', 'padding': '94rpx 0'}"
			:cancel-style="{'font-size': '36rpx', 'color': '#2A2A2A'}"
			:confirm-style="{'font-size': '36rpx', 'color': '#2E73E5'}"
			:show-cancel-button="true"
			cancel-text="再想想" 
			@confirm="loginOut"
		></u-modal>
		<u-modal
			v-model="showVersion"
			content="发现新版本" 
			title="提示"
			:title-style="{'font-size': '36rpx', 'color': '#2A2A2A'}"
			:content-style="{'font-size': '32rpx', 'color': '#4F5053'}"
			:cancel-style="{'font-size': '36rpx', 'color': '#2A2A2A'}"
			:confirm-style="{'font-size': '36rpx', 'color': '#2E73E5'}"
			:show-cancel-button="true"
			cancel-text="再想想"
			confirm-text="立即更新"
			@confirm="confirmUpdate"
		></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: '',
				versionInfo: '',
				showLoginOut: false,
				showVersion: false,
				titleStyle: {'color': '#2A2A2A'},
				isAndroid:false
			}
		},
		onLoad() {
			uni.getSystemInfo({
			    success: (res) => {
					this.$store.commit('saveMoblieInfo', res)
			    }
			});
			// #ifdef APP-PLUS
			if (plus.os.name == 'Android') {
			    this.isAndroid = true;
			}
			// #endif 
		},
		onShow() {
			this.$store.dispatch('getUserInfo').then( res => {
				this.userInfo = res.data.User;
				this.userInfo.Mobile = res.data.User.Mobile.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
			})
		},
		methods: {
			// 退出登录
			loginOut() {
				// 目前仅清除token就完成退出
				uni.removeStorageSync('TOKEN');
				uni.removeStorageSync('USER_ID');
				this.$store.commit('saveUser', null);
				if (plus.os.name == 'Android') {
					this.$store.state.isSocketOpen = false;
					this.$keepAlive.stopKeepAlive();
				}
				// 跳至登录页
				this.$store.dispatch('toLogin');
			},
			
			// 页面跳转
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
			
			// 检查是否有新版本
			checkAppUpdata(){
				let appVersion = '', network = '';
				// 检查网络是否正常
				uni.getNetworkType({
				    success: (res) => {
				        if (res.networkType != 'none') {
							network = true
						}else {
							uni.showToast({
								title: '暂无网络，请检查是否连接网络',
								icon: 'none'
							})
							network = false
						}
				    }
				});
				if (!network) return
				
				// 当前使用的是 android / ios 手机
				uni.getSystemInfo({
				    success: e => {
				        appVersion = e.platform;
				    }
				})
				//如果是安卓直接更新下载
				if (appVersion === 'android') {
					uni.showLoading({ title: '正在检查' });
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					    // 访问版本信息JSON文件
						uni.request({
						    url: this.$store.state.checkVersionUrl,
						    data: {},
						    method: 'GET',
						    success: res => {
								this.versionInfo = res.data;
								console.log(wgtinfo);
								console.log(res.data.Version, wgtinfo.version);
								
								//如果后台版本比当前版本高则更新
								if (res.data.Version.replace(/\.+/g,"") > wgtinfo.version.replace(/\.+/g, "")) {
									this.showVersion = true;
								}else{
								    uni.showToast({icon:'none',title:'当前已是最新版本'});
								}
								uni.hideLoading();
						    },
						    fail: err => {
								uni.showToast({ title: err.message, icon: 'none' });
						    }
						});
					})
				}else {
					//在App Store Connect中的App Store下的app信息，可找到appleId
					let appleId = this.$store.state.iOSAppID;
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
					}, (e) => {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
				
			},
			
			// 确认更新版本
			confirmUpdate() {
				uni.redirectTo({
					url: `/pages/user/updateApp?url=${this.versionInfo.DownloadUrl}`
				})
			},
			
			//允许显示在应用上
			applyOrShowFloatWindow() {
				this.$keepAlive.applyOrShowFloatWindow();
			},
			//设置后台运行
			ignoreBattery() {
				this.$keepAlive.requestIgnoreBatteryOptimizations();
			},
			
			//设置后台运行
			keepLiveSetting() {
				this.$keepAlive.keepLiveSetting();
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.fa-user-list {
		.u-cell {
			padding-left: 0;
			padding-right: 0;
		}
	}
</style>
